#!/usr/bin/env bash
containerName="nginx"
echo '>>> Please later ... ...'
set -e
sudo docker pull nginx:latest | 
   grep "Image is up to date" ||
   (echo Already up to date. Exiting... && exit 0)

echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep ${containerName} | awk '{print $1}')
echo $CID


echo ">>> Stopping and deleting old container ${containerName}"
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi
echo ">>> Starting new container ${containerName}"

docker run --name ${containerName} -p 80:80 -p 443:443 -v /docker/nginx/html:/usr/share/nginx/html -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /docker/nginx/logs:/var/log/nginx -v /docker/nginx/conf.d:/etc/nginx/conf.d  -v /docker/nginx/ssl:/usr/share/nginx/ssl -v /docker/nginx/other:/usr/share/nginx/other -d --restart=always nginx:latest